<?php
// +----------------------------------------------------------------------
// | Demo [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lmx0536.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: limx <715557344@qq.com> <http://www.lmx0536.cn>
// +----------------------------------------------------------------------
// | Date: 2016/4/15 Time: 16:05
// +----------------------------------------------------------------------

namespace Home\Controller;
use Home\Common\AuthController;

class AdminController extends AuthController
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub、
        $this->check_admin_auth(array(1));
    }

    public function index()
    {
        $this -> display();
    }

    public function add($id = "")
    {
        if (fnCheckInts($id)) {
            $M = D("admin");
            $condition ["Id"] = $id;
            $res = $M -> mfnFind($condition);
            if ($res) {
                $this -> assign("info", $res);
            }
        }
        $this -> display();
    }

    /**
     * [pfnDel 删除]
     * @author limx
     * @param $id
     */
    public function pfnDel($id = "")
    {
        if (fnCheckInts($id)) {
            $M = D("admin");
            $res = $M -> mfnDelete($id);
            if ($res) {
                $this -> lfnAjaxSuccess();
            }
            $this -> lfnAjaxFailed($M -> getError());
        } else {
            $this -> lfnAjaxFailed(C("SYSTEM_REFUSE_MSG"));
        }
    }

    /**
     * [pfnGetList 获取信息列表]
     * @author limx
     */
    public function pfnGetList()
    {
        if (IS_POST) {
            $M = D("admin");
            $pageIndex = I("post.pageIndex");
            $pageSize = I("post.pageSize");
            $condition = " 1=1 ";
            $res = $M -> mfnSelect($condition, $pageIndex, $pageSize);
            if ($res) {
                $this -> lfnAjaxSuccess($res);
            }
            $this -> lfnAjaxFailed($M -> getError());
        }
        $this -> lfnAjaxFailed(C("SYSTEM_ERROR_MSG"));
    }

    /**
     * [pfnAddCarLog 新增]
     * @author limx
     */
    public function pfnAdd()
    {
        if (IS_POST) {
            $data ["Id"] = I("post.id");
            $data ["username"] = I("post.username");
            $data ["name"] = I("post.name");
            $pwd=I("post.password");
            if(!empty($pwd)){
                $data ["password"] = MD5($pwd);
            }
            
            $M = D("admin");
            $res = $M -> mfnUpdate($data);
            if ($res !== false) {
                $this -> lfnAjaxSuccess();
            } else {
                $this -> lfnAjaxFailed($M -> getError());
            }
        }
        $this -> lfnAjaxFailed(C("SYSTEM_ERROR_MSG"));
    }

}